package com.jzg.jcptadmin.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

import com.jzg.jcptadmin.ui.MainActivity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyBitmapUtils {

	private LruCache<String, Bitmap> lruCache;
	private Context ctx;
	
	public MyBitmapUtils(Context ctx) {
		this.ctx = ctx;
		
		int maxSize = (int) (Runtime.getRuntime().maxMemory() / 8);
		lruCache = new LruCache<String, Bitmap>(maxSize){
			@Override
			protected int sizeOf(String key, Bitmap value) {
				return value.getRowBytes()*value.getHeight();
			}
		};
		
		cacheFile = ctx.getFilesDir();
		
		executorService = Executors.newFixedThreadPool(3);
	}

	private File cacheFile;
	private ExecutorService executorService;
	public void display(ImageView image, String imageurl, int position) {
		Bitmap bimtap = lruCache.get(imageurl);
		if(bimtap !=null){
			image.setImageDrawable(new BitmapDrawable(bimtap));
			return ;
		}
		String bitmapName = MD5Utils.encode(imageurl);
		File bitmapFile = new File(cacheFile,bitmapName);
		if(bitmapFile.exists() && bitmapFile.length() >0){
			try {
				FileInputStream fin = new FileInputStream(bitmapFile);
				bimtap = BitmapFactory.decodeStream(fin);
				lruCache.put(imageurl, bimtap);
				image.setImageDrawable(new BitmapDrawable(bimtap));

				return ;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		executorService.execute(new DownLoadImageRunnable(image,imageurl,position));
	}
	
	private class DownLoadImageRunnable implements Runnable{

		
		private ImageView image;
		private String imageUrl;
		
		private int position;

		public DownLoadImageRunnable(ImageView image,String imageUrl,int position){
			this.image = image;
			this.imageUrl = imageUrl;
			this.position = position;
		}
		
		@Override
		public void run() {

			try {
				URL url = new URL(imageUrl);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod("GET");
				
				conn.setConnectTimeout(30000);
				conn.setReadTimeout(30000);
				
				int code = conn.getResponseCode();
				if(code == 200){
					InputStream inputStream = conn.getInputStream();
					
					Bitmap bimtap = BitmapFactory.decodeStream(inputStream);
					lruCache.put(imageUrl, bimtap);
					
					MainActivity activity = (MainActivity) ctx;
					activity.runOnUiThread(new Runnable() {
						@Override
						public void run() {

							int currImageViewPosition = (Integer) image.getTag();
							if(currImageViewPosition == position){
								image.setImageDrawable(new BitmapDrawable(lruCache.get(imageUrl)));
							}
						}
					});
					
					String bitmapName = MD5Utils.encode(imageUrl);
					FileOutputStream fout = new FileOutputStream(cacheFile.getAbsolutePath()+"/"+bitmapName);
					bimtap.compress(CompressFormat.JPEG, 100, fout);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
	}
}
